package com.datastructure.test.iscongruent;

import java.util.HashMap;
import java.util.Map;

public class IsCongruent {

    public static void main(String[] args) {
        IsCongruent isCongruent = new IsCongruent();
        int congruent = isCongruent.isCongruent("a", "a");
        System.out.println(congruent);
    }

    public int isCongruent (String s, String c) {
        // write code here
        if(s.length()!=c.length()){
            return -1;
        }
        Map<Byte,Integer> sMap = new HashMap<>();
        Map<Byte,Integer> cMap = new HashMap<>();
        byte[] bytesS = s.getBytes();
        byte[] bytesC = c.getBytes();
        for (byte b : bytesS) {
            sMap.compute(b, (key, value) -> value == null ? 1 : value + 1);
        }
        for (byte b : bytesC) {
            cMap.compute(b, (key, value) -> value == null ? 1 : value + 1);
        }
        for (Map.Entry<Byte, Integer> entry : sMap.entrySet()) {
            if(!cMap.containsKey(entry.getKey())){
                return -1;
            }
            if(!entry.getValue().equals(cMap.get(entry.getKey()))){
                return -1;
            }
        }
        return s.length();
    }
}
